package com.cinsea.ext.cjspx;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.exception.DataOperationException;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;
import com.cinsea.workflow.model.Wfnode;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xf
 * Date: 12-9-24
 * Time: 下午3:16
 */
public class GoodsapplyPX extends BasePX{
    @Override
    public void after(Map context) {
        JdbcDao jdbcDao= Env.getBean("jdbcDao");
        String actiontype= StringUtil.null2String(context.get("actiontype"));
        Wfnode node=(Wfnode)context.get("wfnode");
        String processid=(String)context.get("processid");
        if(actiontype.equals(ActionType.SUBMIT_WFPROCESS)&&node!=null&&node.getId().equals("402880ec3651a95b013652c5a5a60459")){//申请节点提交后增加已领用
            List<Map> shuliangs=jdbcDao.queryForList("select wlname,shuliang from ut_goodscb where processid=?", new Object[]{processid});
            for(Map obj:shuliangs){//循环申请数据
                String wlname=StringUtil.null2String(obj.get("wlname"));
                int applyshuliang= NumberUtil.getIntegerValue(String.valueOf(obj.get("shuliang")));
                if(StringUtil.isEmpty(wlname)) continue;
                List<Map> storelist=jdbcDao.queryForList("select (amountstorage-usesnums) as canuse from ut_storagethinglist where processid=?",new Object[]{wlname});//获取库存数量
                if(storelist.size()>0){
                    Map store=storelist.get(0);
                    int canuse=NumberUtil.getIntegerValue(String.valueOf(store.get("canuse")));
                    if(canuse<applyshuliang){
                        throw new DataOperationException("库存不足。");
                    }else{
                        jdbcDao.update("update ut_storagethinglist set usesnums=usesnums+"+applyshuliang+" where processid=?",new Object[]{wlname});
                    }
                }

            }


        }
    }
}
